package com.ctcemti.mdm.project.quartz.controller;

import com.ctcemti.mdm.common.exception.TaskException;
import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.project.quartz.entity.SysJob;
import com.ctcemti.mdm.project.quartz.service.ISysJobService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 调度任务信息操作处理
 * 
 * @author bxf
 */
@RestController
@RequestMapping("/system/quartz")
public class SysJobController
{

    @Autowired
    private ISysJobService jobService;

    /**
     * 查询定时任务调度对象
     */
    @ApiOperation(value = "查询定时任务调度对象")
    @PostMapping("/query")
    @ApiImplicitParam(name="entity",value = "定时任务调度实体类")
    public Result<SysJob> query(HttpServletRequest request, HttpServletResponse response , Long jobId)
    {
        SysJob job = jobService.selectJobById(jobId);
        return new Result<SysJob>(ResultEnum.SUCCESS,job);
    }

    /**
     * 查询定时任务调度列表
     */
    @ApiOperation(value = "查询定时任务调度列表")
    @PostMapping("/list")
    @ApiImplicitParam(name="job",value = "定时任务调度实体类")
    public Result<PageInfo<SysJob>> queryList(HttpServletRequest request, HttpServletResponse response , SysJob job)
    {
        PageHelper.startPage(job.getPageIndex(),job.getPageSize(),"job_id");
        List<SysJob> list = jobService.selectJobList(job);
        PageInfo<SysJob> pageInfo = new PageInfo(list);
        return new Result<PageInfo<SysJob>>(ResultEnum.SUCCESS,pageInfo);
    }

    /**
     * 新增保存定时任务调度
     */
    @ApiOperation(value = "保存定时任务调度数据")
    @PostMapping("/add")
    @ApiImplicitParams({@ApiImplicitParam(name="entity",value = "定时任务调度数据实体类")})
    public Result<SysJob> insertJob(HttpServletRequest request, HttpServletResponse response ,SysJob job) throws TaskException, SchedulerException {
        jobService.insertJob(job);
        return new Result<SysJob>(ResultEnum.SUCCESS,null);
    }

    /**
     * 修改定时任务调度
     */
    @ApiOperation(value = "更新定时任务调度数据")
    @PostMapping("/update")
    @ApiImplicitParam(name="entity",value = "定时任务调度数据实体类")
    public Result<SysJob> updateJob(HttpServletRequest request, HttpServletResponse response,  SysJob job) throws TaskException, SchedulerException {
        jobService.updateJob(job);
        return new Result<SysJob>(ResultEnum.SUCCESS,null);
    }


    /**
     * 删除定时任务调度
     */
    @ApiOperation(value = "删除定时任务调度")
    @PostMapping("delete")
    @ApiImplicitParams({@ApiImplicitParam(name="ids",value = "主键ids串")})
    public Result<SysJob> remove(HttpServletRequest request, HttpServletResponse response ,String ids) throws SchedulerException {
        jobService.deleteJobByIds(ids);
        return new Result<SysJob>(ResultEnum.SUCCESS,null);
    }

    /**
     * 任务调度状态修改
     */
    @ApiOperation(value = "任务调度状态修改")
    @PostMapping("/changeStatus")
    @ResponseBody
    public Result changeStatus(HttpServletRequest request,SysJob job) throws SchedulerException
    {
        SysJob newJob = jobService.selectJobById(job.getJobId());
        newJob.setStatus(job.getStatus());
        return new Result<SysJob>(ResultEnum.SUCCESS,jobService.changeStatus(newJob));
    }

    /**
     * 任务调度立即执行一次
     */
    @ApiOperation(value = "任务调度状态修改")
    @PostMapping("/run")
    @ResponseBody
    public Result run(HttpServletRequest request,SysJob job) throws SchedulerException
    {
        jobService.run(job);
        return new Result<SysJob>(ResultEnum.SUCCESS,null);
    }

    /**
     * 校验cron表达式是否有效
     */
    @PostMapping("/checkCronExpressionIsValid")
    @ResponseBody
    public boolean checkCronExpressionIsValid(SysJob job)
    {
        return jobService.checkCronExpressionIsValid(job.getCronExpression());
    }
}
